package com.mou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mou.constant.CategoryConstant;
import com.mou.constant.DeletedConstant;
import com.mou.constant.MessageConstant;
import com.mou.dto.CategoryDTO;
import com.mou.dto.CategoryPageQueryDTO;
import com.mou.entity.Category;
import com.mou.exception.CategoryErrorException;
import com.mou.mapper.CategoryMapper;
import com.mou.result.PageResult;
import com.mou.service.CategoryService;
import com.mou.vo.CategoryVO;
import com.mou.vo.ProductCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        long total = page.getTotal();
        List<Category> tempRecords = page.getResult();
        List<CategoryVO> records = new ArrayList<>();
        BeanUtils.copyProperties(tempRecords, records);

        List<Category> allCategory = categoryMapper.selectList(null);

        for (Category c1 : tempRecords) {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(c1, categoryVO);
            if (c1.getParentId() != 0) {
                for (Category c2 : allCategory) {
                    if (c1.getParentId().equals(c2.getId())) {
                        categoryVO.setParentName(c2.getName());
                        records.add(categoryVO);
                    }
                }
            } else {
                categoryVO.setParentName("无");
                records.add(categoryVO);
            }
        }
        return new PageResult(total, records);
    }

    @Override
    public void deleted(Integer id) {
        if (categoryMapper.getCountByParentId(id) == 0) {
            categoryMapper.deleted(id);
        } else {
            throw new CategoryErrorException(MessageConstant.CATEGORY_HAS_CHILDREN);
        }

    }

    @Override
    public void add(CategoryDTO categoryDTO) {
        if (categoryDTO == null) {
            throw new CategoryErrorException(MessageConstant.CATEGORY_NOT_EXIST);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        Category c = categoryMapper.getCategory(category);
        category.setStatus(CategoryConstant.ACTIVE);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setDeleted(DeletedConstant.DELETED_NO);
        if (c == null) {
            categoryMapper.insert(category);
        } else {
            throw new CategoryErrorException(MessageConstant.CATEGORY_EXIST);
        }
    }

    @Override
    public List<ProductCategoryVO> getList() {
        List<Category> categories = categoryMapper.selectList(null);
        List<ProductCategoryVO> productCategoryVOS = new ArrayList<>();
        for (Category c : categories) {
            if (c.getParentId() == 0) {
                if (Objects.equals(c.getStatus(), CategoryConstant.ACTIVE)) {
                    ProductCategoryVO productCategoryVO = new ProductCategoryVO();
                    productCategoryVO.setId(c.getId());
                    productCategoryVO.setName(c.getName());
                    productCategoryVO.setSub(new ArrayList<>());
                    for (Category c1 : categories) {
                        if (Objects.equals(c1.getStatus(), CategoryConstant.ACTIVE) && Objects.equals(c1.getParentId(), c.getId())) {
                            productCategoryVO.getSub().add(c1.getName());
                        }
                    }
                    productCategoryVOS.add(productCategoryVO);
                }
            }
        }
        return productCategoryVOS;
    }
}
